package com.huaxi.ddgw.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.huaxi.ddgw.model.entity.product.Category;
import com.huaxi.ddgw.product.mapper.CategoryMapper;
import com.huaxi.ddgw.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //获取所有一级分类
    @Override
    public List<Category> selectOneCategory() {
        //查询redis 是否有所有一级分类
        String categoryOneJson = redisTemplate.opsForValue().get("category:one");
        //如果redis包含所有一级分类 直接返回
        if (StringUtils.hasText(categoryOneJson)) {
            //categoryOneJson字符串转换List<Category>
            List<Category> existCategoryList = JSON.parseArray(categoryOneJson, Category.class);
            return existCategoryList;
        }
        //如果redis没有所有一级分类 查询数据库 把数据库查询内容返回 并把查询内容放到redis里面
        List<Category> categoryList = categoryMapper.selectOneCategory();
        //放到redis
        redisTemplate.opsForValue().set("category:one", JSON.toJSONString(categoryList), 7, TimeUnit.DAYS);
        return categoryList;
    }

    //查询所有分类 树形结构
    @Cacheable(value = "category", key = "'all'")
    @Override
    public List<Category> findCategoryTree() {
        //查询所有分类 返回list集合
        List<Category> allCategoryList = categoryMapper.findAll();
        //遍历所有分类的list集合 通过parentid=0得到所有一级分类
        List<Category> oneCategoryList = allCategoryList.stream()
                .filter(item -> item.getParentId().longValue() == 0)
                .collect(Collectors.toList());

        if (!CollectionUtils.isEmpty(oneCategoryList)) {
            //遍历所有一级分类 通过id=parentid得到一级下面的二级分类
            oneCategoryList.forEach(oneCategory -> {
                List<Category> twoCategoryList = allCategoryList.stream()
                        .filter(item -> item.getParentId().longValue() == oneCategory.getId().longValue())
                        .collect(Collectors.toList());
                //把二级分类封装到一级分类
                oneCategory.setChildren(twoCategoryList);

                if (!CollectionUtils.isEmpty(twoCategoryList)) {
                    //遍历二级分类 通过id=parentid得到二级下面的三级分类
                    twoCategoryList.forEach(twoCategory -> {
                        List<Category> threeCategoryList = allCategoryList.stream()
                                .filter(item -> item.getParentId().longValue() == twoCategory.getId().longValue())
                                .collect(Collectors.toList());
                        //把三级分类封装到二级分类
                        twoCategory.setChildren(threeCategoryList);
                    });
                }
            });
        }
        return oneCategoryList;
    }
}
